//
//  CartView.m
//  JiaoX
//
//  Created by yinqy on 15/1/13.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "CartView.h"


#define RED_POINT_WIDTH 12

@interface CartView()
{
    UIImageView *_cartImage;
    UILabel *_numLabel;
}
@end
@implementation CartView

//ic_prod_cart
-(instancetype)initWithImage:(NSString *)imageName{
    self = [super init];
    if (self) {
        UIImage *image = [UIImage imageNamed:imageName];
        _cartImage = [[UIImageView alloc]initWithImage:image];
        [_cartImage setFrame:CGRectMake(5, RED_POINT_WIDTH*0.5, image.size.width, image.size.height)];
        [self addSubview:_cartImage];
        
        _numLabel = [[UILabel alloc]init];
        [_numLabel setTextColor:[UIColor whiteColor]];
        [_numLabel setFont:[UIFont systemFontOfSize:8]];
        [_numLabel setFrame:CGRectMake(image.size.width-RED_POINT_WIDTH*0.5, 5, RED_POINT_WIDTH, RED_POINT_WIDTH)];
        [_numLabel setTextAlignment:NSTextAlignmentCenter];
        [_numLabel setHidden:YES];
        
        [_numLabel.layer setCornerRadius:RED_POINT_WIDTH*0.5];
        [_numLabel.layer setBorderColor:RGBColor(204, 204, 204).CGColor];
        [_numLabel.layer setBackgroundColor:[[UIColor redColor] CGColor]];
        
        [self addSubview:_numLabel];
        [self setBounds:CGRectMake(0, 0, image.size.width+RED_POINT_WIDTH*0.5, image.size.height+RED_POINT_WIDTH)];
    }
    return self;
}



-(void)setNum:(NSUInteger)num{
    NSString *numStr = [NSString stringWithFormat:@"%lu",(unsigned long)num];
    if(!num){
        [_numLabel setHidden:YES];
        return;
    }
    [_numLabel setHidden:NO];
    CGFloat width = RED_POINT_WIDTH;
    if(num>99){
        numStr = @"99+";
        CGSize size = [numStr sizeWithFont:[UIFont systemFontOfSize:8]];
        width = size.width+7;
    }
    [_numLabel setFrame:CGRectMake(_cartImage.image.size.width-(width)*0.5, 5, width, RED_POINT_WIDTH)];
    [self setBounds:CGRectMake(0, 0, _cartImage.image.size.width+(width)*0.5, _cartImage.image.size.height+RED_POINT_WIDTH)];
    [_numLabel setText:numStr];
}

-(NSUInteger)num{
    NSUInteger num = 0;
    if(!ISBLANK(_numLabel.text)){
        num = _numLabel.text.integerValue;
    }
    return num;
}

@end
